# remove pdf password
# python pikepdfDe.py filepath

# import pikepdf
# pdf = pikepdf.open('./pdffile.pdf',password='*****')
# pdf.save('E3400_E3600_MCU_Datasheet_Rev00.19unlocked.pdf')

#!/Users/don/anaconda3/bin/python

import os
import sys
import pikepdf


def unlock_file(file):
    pdf = pikepdf.open(file, allow_overwriting_input=True, password='='*****')
    filename = file[:-4]
    pdf.save(filename+'_unlocked.pdf')


def unlock_directory(folder='./'):
    os.chdir(folder)
    filelist = os.listdir()
    for file in filelist:
        if os.path.splitext(file)[1] == '.pdf':
            unlock_file(file)


if __name__ == '__main__':

    print(sys.argv[1]+'\n')
    print(sys.argv[0])

    if len(sys.argv) == 1:
        unlock_directory()
    else:
        target = sys.argv[1]
        if (os.path.isdir(target)):
            unlock_directory(target)
        else:
            unlock_file(target)

    print('done')
